﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;

public partial class VolunteerInvigilation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn;
            string connectionString = ConfigurationManager.ConnectionStrings["ExamScheduleDBConString"].ConnectionString;
            conn = new SqlConnection(connectionString);
            SqlCommand comm = new SqlCommand("sp_getUnvolunteeredExamList", conn);
            comm.CommandType = System.Data.CommandType.StoredProcedure;

            SqlDataReader reader;

            try
            {
                conn.Open();
                reader = comm.ExecuteReader();
                gvUExamList.DataSource = reader;
                gvUExamList.Visible = true;
                gvUExamList.DataBind();
                reader.Close();
            }
            finally
            {
                conn.Close();
            }
        }
    }
    protected void selectRow(object sender, EventArgs e)
    {
        gvUExamList.Visible = false;
        SqlConnection conn;
        string connectionString = ConfigurationManager.ConnectionStrings["ExamScheduleDBConString"].ConnectionString;
        conn = new SqlConnection(connectionString);
        SqlCommand comm = new SqlCommand("SELECT * FROM ExamDetails WHERE CourseID = " + gvUExamList.SelectedValue, conn);
        SqlDataReader reader;
        conn.Open();
        reader = comm.ExecuteReader();
        detailsVolunteer.DataSource = reader;
        detailsVolunteer.DataBind();
        reader.Close();
        conn.Close();

    }
    protected void detailsVolunteer_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        SqlConnection conn;
        SqlConnection conn2;
        string connectionString = ConfigurationManager.ConnectionStrings["ExamScheduleDBConString"].ConnectionString;
        conn = new SqlConnection(connectionString);
        conn2 = new SqlConnection(connectionString);
        SqlCommand updateComm = new SqlCommand("UPDATE ExamDetails SET Additional_InvigilationRequested = 1, RequestFulfilled = 1 WHERE CourseID = " + gvUExamList.SelectedValue, conn);
        SqlCommand updateSP = new SqlCommand("sp_insertNewAdditionalInvigilation", conn);
        updateSP.CommandType = System.Data.CommandType.StoredProcedure;
        updateSP.Parameters.Add("@profName",
               System.Data.SqlDbType.Text);
        updateSP.Parameters.Add("@examID",
        System.Data.SqlDbType.Int);
        conn.Open();
        SqlCommand examComm = new SqlCommand("SELECT * FROM ExamDetails WHERE CourseID = " + gvUExamList.SelectedValue, conn);
        SqlDataReader examReader;
        examReader = examComm.ExecuteReader();
        SqlCommand comm = new SqlCommand("SELECT TOP 1 Professor.Email, Course.CourseName, Course.CourseDescript "
+"FROM Course INNER JOIN Professor ON Course.ProfessorID = Professor.ProfessorID "
+ "WHERE Professor.FirstName = '" + Session["UserName"]+"'", conn2);
        SqlDataReader reader;
        conn2.Open();
        reader = comm.ExecuteReader();

        while (examReader.Read())
        {
            updateSP.Parameters["@profName"].Value = Session["UserName"];
            updateSP.Parameters["@examID"].Value = examReader.GetValue(0);
        }
        examReader.Close();
        updateComm.ExecuteNonQuery();
        updateSP.ExecuteNonQuery();
        while (reader.Read())
        {
            MailMessage message = new MailMessage();
            SmtpClient client;
            message.Priority = MailPriority.Normal;
            message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            message.From = new MailAddress("info@sws320developmentproject.com");
            client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Host = "mail.sws320developmentproject.com";
            client.Port = 25;
            client.EnableSsl = false;
            message.To.Add(new MailAddress(reader.GetValue(0) + ""));
            message.Subject = "SWS320 Development Project - Email - Request For Additional Invigilation - " + reader.GetSqlValue(1);
            message.Body = "THIS IS JUST FOR TESTING PURPOSES." +
                "Your are volunteering in  " + reader.GetSqlValue(2) + ".";
            client.Credentials = new NetworkCredential("info@sws320developmentproject.com ", "INFO320");

            client.Send(message);
            lblMessage.Text = "Request Completed. Confirmation email sent to " + reader.GetValue(0) + "";
        }
        detailsVolunteer.Visible = false;
        //examReader.Close();
        //reader.Close();
        conn.Close();


    }
}